package cn.shop.dys.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.otto.Subscribe;

import cn.shop.dys.bean.UserInfo;
import cn.shop.dys.constant.SPConstant;
import cn.shop.dys.util.BusFactory;
import cn.shop.dys.util.SPUtil;
import cn.shop.dys.util.UserInfoDBUtil;
import cn.shop.dys.widget.CircleImageView;
import cn.shop.dys.widget.MyCircleImageView;
import cn.shop.lib_common.CommonUtil;
import cn.shop.dys.R;
import cn.shop.dys.activity.ImageSelectActivity;


/**
 * 用户详情
 * Created by jian on 2016/4/19.
 */
public class UserDetailsFragment extends BaseFragment implements View.OnClickListener {
    private Context mContext;
    @ViewInject(R.id.rl_details_head)
    private View mDetailsHead;
    @ViewInject(R.id.rl_details_name)
    private View mDetailsName;
    @ViewInject(R.id.rl_details_sex)
    private View mDetailsSex;
    @ViewInject(R.id.rl_details_address)
    private View mDetailsAddress;
    @ViewInject(R.id.iv_details_head)
    private MyCircleImageView mImgDetailsHead;
    @ViewInject(R.id.tv_details_sex)
    private TextView mTVDetailsSex;
    @ViewInject(R.id.tv_details_nick_name)
    private TextView mNickName;
    private OnUserDetailsClickListener mListener;

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user_details, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.mContext = getActivity();
        init();
    }

    private void init() {
        UserInfo userInfo = UserInfoDBUtil.queryUserInfoByUserId(SPUtil.getString(mContext, SPConstant.USER_INFO_USER_ID));
        if(userInfo!=null){
            if(!TextUtils.isEmpty(userInfo.getNickname())){
                Log.d("test","name"+userInfo.getNickname());
                mNickName.setText(userInfo.getNickname());
            }else {
                mNickName.setText(userInfo.getUsername());
            }
            if(!TextUtils.isEmpty(userInfo.getLevel())){
                if(userInfo.getLevel().equals("白银会员")){
                    mImgDetailsHead.setBorderColor(Color.GREEN);
                }else if(userInfo.getLevel().equals("白金会员")){
                    mImgDetailsHead.setBorderColor(Color.BLUE);
                }else if(userInfo.getLevel().equals("铂金会员")){
                    mImgDetailsHead.setBorderColor(Color.RED);
                }else if(userInfo.getLevel().equals("普通会员")){
                    mImgDetailsHead.setBorderColor(Color.WHITE);
                }
            }
        }
        String sex = SPUtil.getString(mContext, SPConstant.USER_SEX);
        if(!TextUtils.isEmpty(sex)){
            mTVDetailsSex.setText(sex);
        }
        initHeadImage();
        initEvent();
    }

    @Override
    public void onResume() {
        super.onResume();
        initHeadImage();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BusFactory.getBus().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }
    @Subscribe
    public void changeNickName(UserInfo userInfo){
        mNickName.setText(userInfo.getNickname());
    }
    private void initHeadImage() {
        if(SPUtil.getBoolean(mContext, SPConstant.USER_INFO_IS_LOGIN)){
            Bitmap headImage = BitmapFactory.decodeFile(CommonUtil.USER_HEAD_IMAGE_PATH);
            if (headImage == null) {
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.inithead);
                mImgDetailsHead.setImageBitmap(bitmap);
                return;
            }
            mImgDetailsHead.setImageBitmap(headImage);
        }else {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.inithead);
            mImgDetailsHead.setImageBitmap(bitmap);
        }
    }

    private void initEvent() {
        mDetailsHead.setOnClickListener(this);
        mDetailsName.setOnClickListener(this);
        mDetailsSex.setOnClickListener(this);
        mDetailsAddress.setOnClickListener(this);
    }

    private String[] sexItems = new String[]{"男", "女"};

    private void showSexDialog() {
        new AlertDialog.Builder(mContext)
                .setTitle("修改性别")
                .setItems(sexItems, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String sexItem = sexItems[which];
                        mTVDetailsSex.setText(sexItem);
                        SPUtil.put(mContext,SPConstant.USER_SEX,sexItem);
                        CommonUtil.showToast(mContext, "修改性别成功");
                    }
                })
                .show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
/**
 * 回调事件处理
 * 作者:孙建
 * created at 2016/4/21 19:21
 */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_details_head:
                Intent intent=new Intent(mContext,ImageSelectActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.rl_details_name:
                if(mListener!=null){
                    mListener.onClick(1);
                }
                break;
            case R.id.rl_details_sex:
                showSexDialog();
                break;
            case R.id.rl_details_address:
                if(mListener!=null){
                    mListener.onClick(2);
                }
                break;

        }
    }

    @Override
    public void setOnUserDetailsClickListener(OnUserDetailsClickListener listener) {
        this.mListener=listener;
    }
}
